/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.tags;

import alcatelparser.modules.AlgoUtil;
import alcatelparser.modules.BuilderFactory;
import alcatelparser.modules.I;

/**
 *
 * @author geniuz
 */
public class CallDurationTag extends DefaultParserTag
{

    public CallDurationTag()
    {
        value = "";
    }
    private String value;
    public final void parseTag(int ai[], int i)
    {
        value = "";
        _array = ai;
        _startParsePosition = i;
        setStandartLength(ai, i);
        if(isFullForm())
        {
            String p1 = getTagPropertiesValue(I.I(6081));
            //value = p1 + " - ";
            BuilderFactory.getBuilder().writeTagContent(3, 0, p1, getTagIdentifyContent(i, i + 1));
        }
        parseCallDurationType(i + 2);
    }
    public String GetResult(){
        return value;
    }
    private void parseCallDurationType(int i)
    {
        int j = _array[i];
        int k = calculateInnerTagType(j);
        if(isFullForm())
        {
            String s = getDecorateDuration(i + 2, AlgoUtil.getLength(_array, i + 1), false);
            String s2 = (new StringBuilder()).append(getTagIdentifyContent(i, i + 1)).append(s).toString();
            BuilderFactory.getBuilder().writeTagContent(4, 0, getTagPropertiesValue((new StringBuilder()).append(I.I(6059)).append(k).toString()), s2);
        } else
        if(isTagChecked(k))
        {
            String s1 = isFieldsDecoding() ? getDecorateDuration(i + 2, AlgoUtil.getLength(_array, i + 1), true) : getHexStringContent(i + 2, AlgoUtil.getLength(_array, i + 1));
            BuilderFactory.getBuilder().writeTagContent(1, 0, getReducedTagPropertiesValue((new StringBuilder()).append(I.I(6059)).append(k).toString()), s1);
        }
    }

    private boolean isTagChecked(int i)
    {
        switch(i)
        {
        case 0: // '\0'
            return isTagChecked(I.I(2779));

        case 1: // '\001'
            return isTagChecked(I.I(2819));

        case 2: // '\002'
            return isTagChecked(I.I(2858));

        case 3: // '\003'
            return isTagChecked(I.I(2899));
        }
        return false;
    }

    private String getDecorateDuration(int i, int j, boolean flag)
    {
        int k = 0;
        String s = "";
        for(int l = 0; l < j; l++)
        {
            int j1 = _array[i + l];
            k <<= 8;
            k += j1;
            s = (new StringBuilder()).append(s).append(toHex(j1)).toString();
        }

        int i1 = k / 100;
        int k1 = k - i1 * 100;
        int l1 = i1 / 3600;
        i1 -= l1 * 3600;
        int i2 = i1 / 60;
        i1 -= i2 * 60;
        String s1 = "";
        if(!flag){
            String p1 = decorateString((new StringBuilder()).append("").append(l1).toString()), p2 = decorateString((new StringBuilder()).append("").append(i2).toString()), p3 = decorateString((new StringBuilder()).append("").append(i1).toString()),p4 = decorateString((new StringBuilder()).append("").append(k1).toString());
            
            value = String.valueOf(Integer.parseInt(p1)*3600+Integer.parseInt(p2)*60+Integer.parseInt(p3))+"~";
                    //"'" + p1+":"+p2+":"+p3 + "'";
            s1 = (new StringBuilder()).append(s).append(I.I(5425)).append(k).append(I.I(5425)).append(p1).append(I.I(465)).append(p2).append(I.I(467)).append(p3).append(I.I(469)).append(p4).toString();
        }
        else
            s1 = (new StringBuilder()).append(decorateString((new StringBuilder()).append("").append(l1).toString())).append(I.I(465)).append(decorateString((new StringBuilder()).append("").append(i2).toString())).append(I.I(465)).append(decorateString((new StringBuilder()).append("").append(i1).toString())).toString();
        return s1;
    }

    private String decorateString(String s)
    {
        if(s.length() < 2)
            return (new StringBuilder()).append(I.I(548)).append(s).toString();
        else
            return s;
    }

    public final String getParseIdentityTag()
    {
        return I.I(6056);
    }
}
